/*
 * Copyright (c) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import 'package:flutter/material.dart';
import 'package:hadss_adaptive_layout/hadss_adaptive_layout.dart';
import 'package:hadss_uni_input/hadss_uni_input.dart';
import 'package:just_audio_ohos/just_audio_ohos.dart';
import 'package:samples/utils/audio_player_manager.dart';

class FloatPlayCardWidget extends StatefulWidget {
  final List<Map> listData;
  final WidthBreakpoint screenType;

  const FloatPlayCardWidget(
      {super.key, required this.screenType, required this.listData});

  @override
  State<FloatPlayCardWidget> createState() => _FloatPlayCardWidgetState();
}

class _FloatPlayCardWidgetState extends State<FloatPlayCardWidget> {
  AudioPlayerManager audioPlayerManager = AudioPlayerManager();

  @override
  Widget build(BuildContext context) {
    return ValueListenableBuilder<Map>(
        valueListenable: audioPlayerManager.currentData,
        builder: (context, value, child) {
          return InkWell(
            onTap: () {
              Navigator.pushNamed(
                context,
                '/musicDetail',
                arguments: {
                  "index": widget.listData.indexOf(value),
                  "dataList": widget.listData,
                },
              );
            },
            child: Container(
              height: 48,
              padding: const EdgeInsets.only(left: 10, right: 10),
              decoration: const BoxDecoration(
                  color: Color(0xffffffff),
                  borderRadius: BorderRadius.all(Radius.circular(8))),
              child: buildCardView(),
            ),
          );
        });
  }

  Widget buildCardView() {
    if (widget.screenType == WidthBreakpoint.md) {
      return buildFoldingScreenView();
    } else if (widget.screenType == WidthBreakpoint.lg ||
        widget.screenType == WidthBreakpoint.xl) {
      return buildPadView();
    }
    return buildMobileView();
  }

  Widget buildMobileView() {
    return Row(children: [
      Image.asset(
        "lib/assets/images/song_cover.png",
        width: 32,
        height: 32,
      ),
      const SizedBox(
        width: 8,
      ),
      buildPlayInfo(),
      const Expanded(child: SizedBox()),
      buildPlayButton(),
      const SizedBox(
        width: 20,
      ),
      Image.asset(
        "lib/assets/images/menu.png",
        width: 24,
        height: 24,
      ),
    ]);
  }

  Widget buildFoldingScreenView() {
    return Row(children: [
      Image.asset(
        "lib/assets/images/song_cover.png",
        width: 32,
        height: 32,
      ),
      const SizedBox(
        width: 8,
      ),
      buildPlayInfo(),
      const Expanded(child: SizedBox()),
      Image.asset(
        "lib/assets/images/loop.png",
        width: 24,
        height: 24,
      ),
      const SizedBox(
        width: 20,
      ),
      buildPlayNext(false),
      const SizedBox(
        width: 20,
      ),
      buildPlayButton(),
      const SizedBox(
        width: 20,
      ),
      buildPlayNext(true),
      const SizedBox(
        width: 20,
      ),
      Image.asset(
        "lib/assets/images/menu.png",
        width: 24,
        height: 24,
      ),
    ]);
  }

  Widget buildPadView() {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        Row(
          children: [
            Image.asset(
              "lib/assets/images/song_cover.png",
              width: 32,
              height: 32,
            ),
            const SizedBox(
              width: 8,
            ),
            buildPlayInfo(),
          ],
        ),
        Row(
          children: [
            Image.asset(
              "lib/assets/images/loop.png",
              width: 24,
              height: 24,
            ),
            const SizedBox(
              width: 20,
            ),
            buildPlayNext(false),
            const SizedBox(
              width: 20,
            ),
            buildPlayButton(),
            const SizedBox(
              width: 20,
            ),
            buildPlayNext(true),
            const SizedBox(
              width: 20,
            ),
            Image.asset(
              "lib/assets/images/menu.png",
              width: 24,
              height: 24,
            ),
          ],
        ),
        Image.asset(
          "lib/assets/images/heart_unselect.png",
          width: 24,
          height: 24,
        ),
      ],
    );
  }

  Widget buildPlayInfo() {
    return ValueListenableBuilder<Map>(
      valueListenable: audioPlayerManager.currentData,
      builder: (context, value, child) {
        return Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text(
              value["title"] ?? "Dream it possible",
              style: const TextStyle(
                  fontSize: 14,
                  fontWeight: FontWeight.w500,
                  color: Colors.black),
            ),
            Text(
              value["subtitle"] ?? "Delaney",
              style: const TextStyle(fontSize: 10, color: Color(0x99000000)),
            ),
          ],
        );
      },
    );
  }

  Widget buildPlayButton() {
    return StreamBuilder<PlayerState>(
      stream: audioPlayerManager.audioPlayer.playerStateStream,
      builder: (context, snapshot) {
        final playerState = snapshot.data;
        final playing = playerState?.playing;
        if (playing == true) {
          return UnifiedGestureDetector(
            onTap: audioPlayerManager.pause,
            child: Image.asset(
              "lib/assets/images/play_icon.png",
              width: 24,
              height: 24,
            ),
          );
        } else {
          return UnifiedGestureDetector(
            onTap: audioPlayerManager.play,
            child: Image.asset(
              "lib/assets/images/pause_icon.png",
              width: 24,
              height: 24,
            ),
          );
        }
      },
    );
  }

  Widget buildPlayNext(bool isNext) {
    return isNext
        ? UnifiedGestureDetector(
            onTap: audioPlayerManager.playNext,
            child: Image.asset(
              "lib/assets/images/ic_public_play_next.png",
              width: 24,
              height: 24,
            ),
          )
        : UnifiedGestureDetector(
            onTap: audioPlayerManager.playPrevious,
            child: Image.asset(
              "lib/assets/images/ic_public_play_before.png",
              width: 24,
              height: 24,
            ),
          );
  }
}
